var UpdateInfoDlg = {CustomerInfoData: {}, zTreeInstance: null, validateFields: {}};
//清除数据
UpdateInfoDlg.clearData = function () {
    this.customerInfoData = {};
};
//设置对话框中的数据 @param key 数据的名称 @param val 数据的具体值
UpdateInfoDlg.set = function (key, val) {
    this.customerInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    return this;
};
//设置对话框中的数据 @param key 数据的名称 @param val 数据的具体值
UpdateInfoDlg.get = function (key) {
    return $("#" + key).val();
};
//关闭此对话框
UpdateInfoDlg.close = function () {
    parent.layer.close(parent.layer.getFrameIndex(window.name));
};
//收集数据
UpdateInfoDlg.collectData = function () {
    this.set('id').set('oneChannel').set('twoChannel').set('thrChannel').set('sOneChannel').set('sTwoChannel').set('sThrChannel').set('archiving').set('remarks').set('name');
};
//验证数据是否为空
UpdateInfoDlg.validate = function () {
    $('#updateInfoForm').data("bootstrapValidator").resetForm();
    $('#updateInfoForm').bootstrapValidator('validate');
    return $("#updateInfoForm").data('bootstrapValidator').isValid();
};

function editInformation() {
    $("#editInformation").find('input,select').addClass('styleShow');
    $("#remarks").addClass('styleShow');
    if ($("#oneChannelValue").val() == "直接上门") {
        $("#oneMedium,#twoMedium,#thrMedium").addClass('styleShow');
    }
}

function updateCustomer() {
    var customerName = $("#customerName").val();                   //建档人
    var customerSex = $("#customerSex").val();     //原一级渠道
    var province = $("#province").val();     //原二级渠道
    var cities = $("#cities").val();     //原三级渠道
    var region = $("#region").val();         //提交人
    var address = $("#address").val();       //一级渠道
    var birthday = $("#birthday").val();       //二级渠道
    var customerAge = $("#customerAge").val();       //三级渠道
    var medicalRecordNumber = $("#medicalRecordNumber").val();             //备注
    var occupation = $("#occupation").val();
    var qqNumber = $("#qqNumber").val();
    var wechatNumber = $("#wechatNumber").val();
    var idCardNo = $("#idCardNo").val();
    var maritalStatus = $("#maritalStatus").val();
    var referee = $("#referee").val();
    var refereeShip = $("#refereeShip").val();
    var yellowNumber = $("#yellowNumber").val();
    var oneMedium = $("#oneMedium").val();
    var twoMedium = $("#twoMedium").val();
    var thrMedium = $("#thrMedium").val();
    var oneMediumValue = $("#oneMediumValue").val();
    var twoMediumValue = $("#twoMediumValue").val();
    var thrMediumValue = $("#thrMediumValue").val();
    var remarks = $("#remarks").val();
    var isHaveChildren = $("#isHaveChildren").val();
    var reservationsId = $("#reservationsId").val();
    var customerId = $("#customerId").val();
    var customerPhone = $("#customerPhoneValue").val();
    var orthodonticNumber = $("#orthodonticNumber").val();  //正畸编号
    var orthodonticNumberValue = $("#orthodonticNumberValue").val();  //正畸编号
    var isUpdaMediu = 1;
    if (oneMedium != oneMediumValue || twoMedium != twoMediumValue || thrMedium != thrMediumValue) {
        isUpdaMediu = 2;
    }
    if (customerName == null || customerName == "") {
        alert("请输入姓名！");
        return;
    }
    if (customerAge == null || customerAge == "") {
        alert("请输入年龄！");
        return;
    }
    if (birthday == null || birthday == "") {
        alert("请输入生日！");
        return;
    }
    var resbirthday = /^(19\d\d|20\d\d)[-/ ](0\d|1[0-2])[-/ ](0\d|[1-2]\d|30|31)$/;
    if (resbirthday.test(birthday) == false) {
        alert("生日格式不正确，请输入正确的格式，例xxxx-xx-xx");
        return;
    }
    if (province == null || province == "" || province == "请选择") {
        alert("请选择省份！");
        return;
    }
    if (cities == null || cities == "" || cities == "请选择") {
        alert("请选择城市！");
        return;
    }
    if (medicalRecordNumber == null || medicalRecordNumber == "") {
        alert("请输入病历号！");
        return;
    }
    $.ajax({
        url: Feng.ctxPath + "/customer/updateCustomeInfoValue",
        type: "post",
        data: {
            customerName: customerName,
            customerPhone: customerPhone,
            customerSex: customerSex,
            province: province,
            cities: cities,
            region: region,
            address: address,
            birthday: birthday,
            customerAge: customerAge,
            medicalRecordNumber: medicalRecordNumber,
            occupation: occupation,
            qqNumber: qqNumber,
            wechatNumber: wechatNumber,
            maritalStatus: maritalStatus,
            referee: referee,
            refereeShip: refereeShip,
            yellowNumber: yellowNumber,
            idCardNo: idCardNo,
            oneMedium: oneMediumValue,
            twoMedium: twoMediumValue,
            thrMedium: thrMediumValue,
            remarks: remarks,
            isHaveChildren: isHaveChildren,
            reservationsId: reservationsId,
            isUpdaMediu: isUpdaMediu,
            orthodonticNumber:orthodonticNumber,
            orthodonticNumberValue:orthodonticNumberValue,
            id: customerId
        },
        success: function (data) {
            if (data == "0") {
                alert("病历号已存在！");
            } else {
                Feng.success('操作成功!');
                window.parent.Customer.searchrefresh();
                $("#editInformation").find('input,select').removeClass('styleShow');
                $("#remarks").removeClass('styleShow');
                $("#oneMedium,#twoMedium,#thrMedium").removeClass('styleShow');
            }
        }, error: function (data) {
            alert('没有权限，请联系系统管理员!');
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        }
    });
}

$(function () {
    $(".modify").attr("readOnly", "true");
    $(".select_styles").attr("disabled", "disabled");
    $(".select_styles").attr("disabled", "disabled");
    $(".frameBox").attr("disabled", "disabled").css("border", "none");
    var seleterefereeShip = $('#refereeShipValue').val();
    if (seleterefereeShip != "") {
        $("#refereeShip option[value='" + seleterefereeShip + "']").attr("selected", "selected");
    }
    var seleteoccupation = $('#occupationValue').val();
    if (seleteoccupation != "") {
        $("#occupation option[value='" + seleteoccupation + "']").attr("selected", "selected");
    }
    if ($("#customerSexValue").val() == "男") {
        $("#customerSex option[value='1']").attr("selected", "selected");
    } else if ($("#customerSexValue").val() == "女") {
        $("#customerSex option[value='2']").attr("selected", "selected");
    }
    var seletemaritalStatus = $('#maritalStatusValue').val();
    if ($('#maritalStatusValue').val() != "") {
        $("#maritalStatus option[value='" + seletemaritalStatus + "']").attr("selected", "selected");
    }
    var seleteisHaveChildren = $('#isHaveChildrenValue').val();
    if ($('#isHaveChildrenValue').val() != "") {
        $("#isHaveChildren option[value='" + seleteisHaveChildren + "']").attr("selected", "selected");
    }
    $("#birthday").on("input", function () {
        var resbirthday = /^(19\d\d|20\d\d)[-/ ](0\d|1[0-2])[-/ ](0\d|[1-2]\d|30|31)$/;
        var birthday = $("#birthday").val();
        if (birthday.length > 0 && birthday.length < 4) {
        } else if (birthday.length >= 4 && birthday.length < 10) {
            var s = birthday.substring(0, 4);
            if (!isNaN(s)) {
                var data = new Date();
                $("#customerAge").val(data.getFullYear() - s);
            }
        } else {
            if (resbirthday.test(birthday)) {
                var birthDayTime = new Date(birthday).getTime();
                var nowTime = new Date().getTime();
                $("#customerAge").val(Math.ceil((nowTime - birthDayTime) / 31536000000));
            } else {
                Feng.error("请输入正确的格式！");
            }
        }
    });
    $("#customerAge").on('input', function () {
        var date = new Date();
        var currentYear = date.getFullYear() - $("#customerAge").val();
        $("#birthday").val(currentYear + "-01-01");
    });
});